package cn.sfturing.service.impl;

import java.util.List;
import java.util.Map;

import cn.sfturing.dao.OfficeDao;
import cn.sfturing.entity.Office;
import cn.sfturing.utils.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.sfturing.dao.DoctorDao;
import cn.sfturing.entity.Doctor;
import cn.sfturing.entity.Hospital;
import cn.sfturing.service.DoctorService;

@Service
public class DoctorServiceImpl implements DoctorService {

	@Autowired
	private DoctorDao doctorDao;
	@Autowired
	private OfficeDao officeDao;

	@Override
	public List<Doctor> findAreaByHosAndOfficeName(String hospitalName, String OfficeName) {
		// TODO Auto-generated method stub
		return doctorDao.findAreaByHosAndOfficeName(hospitalName, OfficeName);
	}

	// 查询医生
	@Override
	public Doctor findDoctorById(int id) {
		// TODO Auto-generated method stub
		return doctorDao.findDoctorById(id);
	}

	@Override
	public Doctor findDoctorByName(String name,String office,String hospital){
		return doctorDao.findDoctorByName(name, office, hospital);
	}

	@Override
	public List<Doctor> findDoctorByRe(Map<String, Object> doctorMap) {
		// TODO Auto-generated method stub
		return doctorDao.findDoctorByRe(doctorMap);
	}

	@Override
	public int findDoctorByReNum(List<Hospital> hospital) {
		// TODO Auto-generated method stub
		return doctorDao.findDoctorByReNum(hospital);
	}

	@Override
	public List<Doctor> findDoctorByCondition(Doctor doctor, int start, int size) {
		if (doctor.getDoctorAdministrative() != null && doctor.getDoctorAdministrative().equals("默认")) {
			doctor.setDoctorAdministrative(null);
		}
		if (doctor.getDoctorTitle() != null && doctor.getDoctorTitle().equals("默认")) {
			doctor.setDoctorTitle(null);
		}
		if (doctor.getDoctorDegree() != null && doctor.getDoctorDegree().equals("默认")) {
			doctor.setDoctorDegree(null);
		}
		return doctorDao.findDoctorByCondition(doctor.getHospitalName(), doctor.getOfficesName(), doctor.getDoctorName(),
				doctor.getDoctorAdministrative(), doctor.getDoctorTitle(), doctor.getDoctorDegree(),start,size);
	}

	@Override
	public int findDoctorNum(Doctor doctor) {
		if (doctor.getDoctorAdministrative() != null && doctor.getDoctorAdministrative().equals("默认")) {
			doctor.setDoctorAdministrative(null);
		}
		if (doctor.getDoctorTitle() != null && doctor.getDoctorTitle().equals("默认")) {
			doctor.setDoctorTitle(null);
		}
		if (doctor.getDoctorDegree() != null && doctor.getDoctorDegree().equals("默认")) {
			doctor.setDoctorDegree(null);
		}
		return doctorDao.findDoctorNum(doctor.getHospitalName(), doctor.getOfficesName(), doctor.getDoctorName(),
				doctor.getDoctorAdministrative(), doctor.getDoctorTitle(), doctor.getDoctorDegree());
	}

	@Override
	public List<String> findDoctorTitle() {
		// TODO Auto-generated method stub
		return doctorDao.findDoctorTitle();
	}

	@Override
	public List<String> findDoctorAdministrative() {
		// TODO Auto-generated method stub
		return doctorDao.findDoctorAdministrative();
	}

	@Override
	public List<String> findDoctorDegree() {
		// TODO Auto-generated method stub
		return doctorDao.findDoctorDegree();
	}

	@Override
	public List<Doctor> everythingQuery(Doctor query) {
		return doctorDao.everythingQuery(query);
	}

    @Override
    public Boolean byIdDel(Integer id) {
		Boolean TF=false;
		Doctor doctor = doctorDao.findDoctorById(id);
		if(doctorDao.byIdDel(id) > 0){
			Office office = officeDao.findOfficeByHosNameAndOfficeName(doctor.getHospitalName(), doctor.getOfficesName());
			if (null != office) {
				String str=office.getDoctorNum().substring(0, office.getDoctorNum().length() - 1);
				office.setDoctorNum(String.valueOf(Integer.parseInt(str)-1)+"人");
				officeDao.updateOfficeById(office);
			}
			return true;
		}
        return TF;
    }

	@Override
	public List<Doctor> queryBk(Doctor doctor){
		return doctorDao.queryBk(doctor);
	}

	@Override
	public boolean updataAll(Doctor doctor) {
		return doctorDao.updataAll(doctor)>0?true:false;
	}

	@Override
	public boolean allDoctor(Doctor doctor) {
		boolean tf=false;
		Office office = officeDao.findOfficeByHosNameAndOfficeName(doctor.getHospitalName(), doctor.getOfficesName());
		// 初始化密码
		if(doctor.getPasswrod()!=null &&doctor.getPasswrod()!=""){
			doctor.setPasswrod(MD5.getMD5(doctor.getPasswrod()));
		}else {
			doctor.setPasswrod("123456");
		}
		// 更改科室人数
		if(doctorDao.allDoctor(doctor)>0){
			if(null != office){
				String str=office.getDoctorNum().substring(0,office.getDoctorNum().length() - 1);
				office.setDoctorNum(String.valueOf(Integer.parseInt(str)+1)+"人");
				officeDao.updateOfficeById(office);
			}
			tf=true;
		}
		return tf;
	}

	@Override
	public boolean updataPassword(Doctor doctor,String newPassword) {
		boolean tf=false;
		if(doctor.getPasswrod()!=null && doctor.getPasswrod()!=""){
			doctorDao.updataPassword(doctor,newPassword);
			tf=true;
		}else {
			doctorDao.updataPassword(doctor,newPassword);
			tf=true;
		}
		return tf;
	}

	@Override
	public Doctor loginDoctor(Doctor doctor) {
		return doctorDao.loginDoctor(doctor);
	}
}
